﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimationCtrl : MonoBehaviour
{
    private Animator animator;

    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        Movement(KeyCode.W, 1, 0);
        Movement(KeyCode.A, 2, 1);
        Movement(KeyCode.S, 3, 1);
        Movement(KeyCode.D, 4, 1);
        Action(KeyCode.Alpha1, 6);
        Action(KeyCode.Alpha2, 7);
        Action(KeyCode.Alpha3, 8);
        Action(KeyCode.Space, 9);
    }

    private void Movement(KeyCode key, int state, int exit)
    {
        if (Input.GetKey(key))
        {
            animator.SetInteger("state", state);
            if (Input.GetKeyDown(KeyCode.LeftShift))
            {
                animator.SetBool("running", !animator.GetBool("running"));
            }
            // Slide
            if (animator.GetBool("running"))
            {
                if (Input.GetKeyDown(KeyCode.LeftControl))
                {
                    animator.SetTrigger("slide");
                }
            }
        }
        else if (Input.GetKeyUp(key))
        {
            // Running -> Wait cause tired
            if (animator.GetBool("running") && exit == 0)
            {
                // Tired
                animator.SetInteger("state", 5);
                animator.SetBool("running", false);
            }
            else
            {
                animator.SetInteger("state", exit);
            }
        }
    }

    private void Action(KeyCode key, int state)
    {
        if (Input.GetKeyDown(key))
        {
            animator.SetInteger("state", state);
        }
        else if (Input.GetKeyUp(key))
        {
            animator.SetInteger("state", 0);
        }
    }
}
